import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { EventProvider } from '../../providers/event/event';
import { App } from 'ionic-angular';
import { AppVersion } from '@ionic-native/app-version';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import { LoadingController, Loading } from 'ionic-angular';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { UserDataService } from '../../providers/service/user-data.service';
import { Network } from '@ionic-native/network';

/**
 * Generated class for the SettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
})
export class SettingPage {

  version = '1.0.0';
  sendStatus = 0;

  constructor(
    public navCtrl: NavController,
    public viewCtrl: ViewController,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider,
    public eventsPvd: EventProvider,
    private app: App,
    private appVersion: AppVersion,
    private iab: InAppBrowser,
    public loadingCtrl: LoadingController,
    public user: UserInfoProvider,
    public userData: UserDataService,
    private network: Network,) { }

  async ionViewDidLoad() {
    console.log('ionViewDidLoad SettingPage');
    this.appVersion.getVersionNumber().then((ver) => {
      this.version = ver;
    });
  }

//skipToHelpPage() {
//  this.navCtrl.push('HelpPage');
//}

  skipToAccount() {
    this.sendStatus++;
    this.navCtrl.push('ChangepwdPage', {
      sendStatus: this.sendStatus
    });
  }
//skipToQuestionAndAnswerPage() {
//  this.navCtrl.push('QuestionAndAnswerPage');
//}
  skipToAboutUsPage() {
    this.navCtrl.push('AboutUsPage');
  }

  //注销登录
  async openLoginOut() {
    this.dialog.showConfirm('确定要退出登录吗？', '').then( async (success) => {
      await this.userData.deleteAll();
      let loading = this.loadingCtrl.create({
        content: '退出中...'
      });
      loading.present();
      let userInfo = this.user.getuserInfo();
      // userInfo.isAutomaticLogon = false;
      await this.userData.save(userInfo);
      
      if (this.network.type === 'none' || this.network.type === '2g') {
        loading.dismiss();
        this.app.getRootNav().setRoot('LoginPage');
        return;
      }
      //调用接口的方法
      this.http.postJson('', 'users/member/logout',
        {

        }, '').then(res => {
          loading.dismiss();
          if (res['status'] == 200) {
            this.app.getRootNav().setRoot('LoginPage');
          }
        });
    }, fail => {
      //什么都不做
    });

    // 	//确认对话框
    // 	this.dialog.showConfirm('提示', '确定要退出登录吗？').then(success => {
    //     this.storage.removeStorage("login_info");
    //      //this.eventsPvd.sendEvents(this.eventsPvd.EVENT_LOGIN, {});
    //      this.app.getRootNav().setRoot('LoginPage');
    // 	}, fail => {
    //  		//什么都不做
    //  	});

  }

  getversion() {
    this.http.postJson('', 'apis/apis/getversion',
      {
        version: this.version,
      }, '')
      .then(res => {
        if (res['status'] == 200) {
          // thid dd=res['data']['is_new'];
          if (res['data']['is_new'] === true) {
            if (res['data']['appUpdate'] === true) {
              this.dialog.showAlert('当前版本已经无法使用，请下载最新!', '').then(success => {
                this.iab.create('https://gx.mzzckj.com/download/', '_system', 'location=yes');
              });
              this.openLoginOut()
              return;
            }


            this.dialog.showConfirm('有新的版本需要更新么?', '').then(success => {
              this.iab.create('https://gx.mzzckj.com/download/', '_system', 'location=yes');
            }, fail => {
              //什么都不做
            });
          } else {
            this.dialog.showAlert('已经是最新版本!', '')
          }
        }
      });
  }



}
